using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web.Security;
using System.Configuration;
using System.Web.Configuration;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace Starznet.SharePoint.SPDAT.ApplicationPages.Settings
{
    public class MembershipRoleListPage : LayoutsPageBase
    {
        protected SPGridView GvFormsAuthRoleList;
        private MenuTemplate _oRoleListMenu;
        private DataView _oView;
        protected ToolBar tbUserList;
        
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            //GvFormsAuthRoleList.Sorting += new GridViewSortEventHandler(GvFormsAuthUserList_Sorting);

            GvFormsAuthRoleList.Controls.Clear();

            _oRoleListMenu = new MenuTemplate();
            _oRoleListMenu.ID = "RoleListMenu";

            SPMenuField oRoleMenu = new SPMenuField();
            oRoleMenu.HeaderText = this.GetGlobalResourceObject("szadm", "RoleEdit_RoleNameLabel").ToString();
            oRoleMenu.TextFields = "RoleName";
            oRoleMenu.MenuTemplateId = "RoleListMenu";
            oRoleMenu.NavigateUrlFields = "RoleName";
            oRoleMenu.NavigateUrlFormat = "MembershipViewRole.aspx?RoleName={0}";
            oRoleMenu.TokenNameAndValueFields = "NAME=RoleName";
            oRoleMenu.SortExpression = "RoleName";

            MenuItemTemplate oViewRole = new MenuItemTemplate(this.GetGlobalResourceObject("szadm", "RoleList_ViewRoleMenu").ToString(), "/_layouts/images/EDIT.GIF");
            oViewRole.ClientOnClickNavigateUrl = "MembershipViewRole.aspx?RoleName=%NAME%";
            _oRoleListMenu.Controls.Add(oViewRole);

            MenuItemTemplate oDeleteRole = new MenuItemTemplate(this.GetGlobalResourceObject("szadm", "RoleList_DeleteRoleMenu").ToString(), "/_layouts/images/DELITEM.GIF");
            oDeleteRole.ClientOnClickNavigateUrl = "MembershipDeleteRole.aspx?RoleName=%NAME%";
            _oRoleListMenu.Controls.Add(oDeleteRole);

            this.Page.Controls.Add(_oRoleListMenu);
            GvFormsAuthRoleList.Columns.Add(oRoleMenu);

            BoundField oCountField = new BoundField();
            oCountField.DataField = "UserCount";
            oCountField.HeaderText = this.GetGlobalResourceObject("szadm", "RoleList_UserCountColumn").ToString();
            oCountField.SortExpression = "UserCount";
            GvFormsAuthRoleList.Columns.Add(oCountField);

            this.PopulateRoleListData();
        }

        private void PopulateRoleListData()
        {
            DataTable oDT = new DataTable();
            oDT.Columns.Add("RoleName");
            oDT.Columns.Add("UserCount");

            foreach (String sRole in Roles.GetAllRoles())
            {
                DataRow oRow = oDT.NewRow();
                oRow["RoleName"] = sRole;

                //Determine the number of users in the rolw
                oRow["UserCount"] = Roles.GetUsersInRole(sRole).GetUpperBound(0) + 1;

                oDT.Rows.Add(oRow);
            }
            _oView = new DataView(oDT);
            GvFormsAuthRoleList.DataSource = _oView;
            GvFormsAuthRoleList.DataBind();
        }
    }
}
